<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace Plugin\Alarm\Repository;

use App\Repository\IRepository;
use Hyperf\Collection\Arr;
use Hyperf\Database\Model\Builder;
use Plugin\Alarm\Model\RdAlarmWarnRecord;

/**
 * 报警记录 Repository类
 */
class RdAlarmWarnRecordRepository extends IRepository
{
    public function __construct(
        protected readonly RdAlarmWarnRecord $model
    )
    {
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {

        // 
        if (isset($params['id']) && filled($params['id'])) {
            $query->where('id', '=', $params['id']);
        }

        // 广播id
        if (isset($params['broadcast_id']) && filled($params['broadcast_id'])) {
            $query->where('broadcast_id', '=', $params['broadcast_id']);
        }

        // 消息id
        if (isset($params['msg_id']) && filled($params['msg_id'])) {
            $query->where('msg_id', '=', $params['msg_id']);
        }

        // 装置id
        if (isset($params['rod_id']) && filled($params['rod_id'])) {
            $query->where('rod_id', '=', $params['rod_id']);
        }

        // 站点id
        if (Arr::get($params, 'stand_id')) {
            $query->whereIn('stand_id', Arr::wrap($params['stand_id']));
        }

        if (Arr::get($params, 'type')) {
            $query->whereIn('type', Arr::wrap($params['type']));
        }
        
        // 风险等级
        if (Arr::get($params, 'risk_level')) {
            $query->whereIn('risk_level',  Arr::wrap($params['risk_level']));
        }
        
        // 标题搜索
        if (Arr::get($params, 'title')) {
            $query->where('title', 'like', '%' . $params['title'] . '%');
        }
        
        // 内容搜索
        if (Arr::get($params, 'content')) {
            $query->where('content', 'like', '%' . $params['content'] . '%');
        }
        
        // 消息类型
        if (Arr::get($params, 'msg_type')) {
            $query->where('msg_type', '=', $params['msg_type']);
        }

        if (Arr::get($params, 'start_time') && Arr::get($params, 'end_time')) {
            $query->whereBetween(
                'warning_time',
                [$params['start_time'], $params['end_time']]
            );
        }

        return $query;
    }

    public function page(array $params = [], ?int $page = null, ?int $pageSize = null): array
    {
        $result = $this->perQuery($this->getQuery(), $params)
            ->with(['device:id,name,addr', 'rod:id,name', 'stand:id,name', 'type:id,name,level,type', 'msg:id,file_url,alarm_pic_path'])
            ->paginate(
                perPage: $pageSize,
                pageName: static::PER_PAGE_PARAM_NAME,
                page: $page,
            );
        return $this->handlePage($result);
    }

    public function detail(int $id)
    {
        return $this->getQuery()
            ->with(['device:id,name,addr', 'rod:id,name', 'stand:id,name', 'type:id,name,risk_level,type', 'msg'])
            ->findOrFail($id);
    }
}